﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using QLNHang.DTO;
using QuanLyDatVe.DataAccessLayer;
using System.Data.SqlClient;
using System.Data;
namespace QLNHang.DAO
{
    class HoaDonDAO
    {

        private ConnectDatabase cn = new ConnectDatabase();
        DBNull dbNULL = DBNull.Value;
        public int ThemHoaDon(HoaDonDTO hoaDonDTO)
        {
            cn.OpenDatabase();
            SqlCommand cmd = new SqlCommand("sp_ThemHoaDon", cn.SqlConnect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@maKhachHang", string.IsNullOrEmpty(hoaDonDTO.MaKhachHang) ? dbNULL : (object)hoaDonDTO.MaKhachHang);
            cmd.Parameters.AddWithValue("@maVe", string.IsNullOrEmpty(hoaDonDTO.MaVe) ? dbNULL : (object)hoaDonDTO.MaVe);
            cmd.Parameters.AddWithValue("@soLuong", string.IsNullOrEmpty(hoaDonDTO.SoLuong) ? dbNULL : (object)hoaDonDTO.SoLuong);
            cmd.Parameters.AddWithValue("@diemTichLuy", string.IsNullOrEmpty(hoaDonDTO.DiemTichLuy) ? dbNULL : (object)hoaDonDTO.DiemTichLuy);
                 
            int success = cmd.ExecuteNonQuery();
            cn.CloseDatabase();
            return success;
        }
        public int CapNhatDiemTichLuy(HoaDonDTO hoaDonDTO)
        {
            cn.OpenDatabase();
            SqlCommand cmd = new SqlCommand("sp_CapNhatDiemTichLuy", cn.SqlConnect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@maKH", string.IsNullOrEmpty(hoaDonDTO.MaKhachHang) ? dbNULL : (object)hoaDonDTO.MaKhachHang);
            cmd.Parameters.AddWithValue("@diemTichLuy", string.IsNullOrEmpty(hoaDonDTO.DiemTichLuy) ? dbNULL : (object)hoaDonDTO.DiemTichLuy);

            int success = cmd.ExecuteNonQuery();
            cn.CloseDatabase();
            return success;
        }
        public int SuaHoaDon(HoaDonDTO hoaDonDTO)
        {
            cn.OpenDatabase();
            SqlCommand cmd = new SqlCommand("sp_SuaHoaDon", cn.SqlConnect);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@maHoaDon", string.IsNullOrEmpty(hoaDonDTO.MaHoaDon) ? dbNULL : (object)hoaDonDTO.MaHoaDon);
            cmd.Parameters.AddWithValue("@maKhachHang", string.IsNullOrEmpty(hoaDonDTO.MaKhachHang) ? dbNULL : (object)hoaDonDTO.MaKhachHang);
            cmd.Parameters.AddWithValue("@maVe", string.IsNullOrEmpty(hoaDonDTO.MaVe) ? dbNULL : (object)hoaDonDTO.MaVe);
            int success = cmd.ExecuteNonQuery();
            cn.CloseDatabase();
            return success;
        }
        public DataTable XemHoaDon(HoaDonDTO hoaDonDTO)
        {
            var dt = new DataTable();
            cn.OpenDatabase();
            SqlCommand cmd = new SqlCommand("sp_XemHoaDon", cn.SqlConnect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@maKhachHang", string.IsNullOrEmpty(hoaDonDTO.MaKhachHang) ? dbNULL : (object)hoaDonDTO.MaKhachHang);
            var sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
            cn.CloseDatabase();
            return dt;
        }
    }
}
